Vue.filter('handleTime', (time)=>{
    return time1(time)
    function time1(t) {
        let s  = t.slice(0, 10)
        let year = Number(s.split('/')[0]) 
        let month = Number(s.split('/')[1])
        let day = Number(s.split('/')[2])
        let time1 = getDays(year, month, day) // 目标时间
        let time2 = getDays(vm.now.year, vm.now.month, vm.now.day) //参照时间
        if (year === vm.now.year) {//本年
            if (vm.now.year === year && vm.now.month === month && vm.now.day === day){ //当天
                return t.slice(11, 16) //只返回 时:分
            }else if (vm.now.year === year && time2 - time1 === 1) { //昨天(仅处理同年同月场景)
                return `昨天${t.slice(11, 16)}`  //返回 昨天+时:分
            }else if (vm.now.year === year && time2 - time1 > 1) {
                return t.slice(5, 16) //只返回 月/日 时:分
            }else{ //昨天以前
                return t.slice(5, 16) //只返回 月/日 时:分
            }
        }else{//往年
            return t.slice(0, 16) //返回完整时间 年/月/日 时:分
        } 
    }/**/
    function isfullyear(year){
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            return true;
        }
    }
    function getDays(year, month, day){
        //把天数赋值给num
        var num = day;
        //如果用户输入1月 则直接返回天数
        if (month == 1) {
            return day;
        }
        var monthDay = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
        //循环遍历月份 循环条件是用户输入月份的前一个月份 
        for (var i = 0; i < month - 1; i++) {
            //num = 当月天数 + 之前每个月的天数
            num = num + monthDay[i]
        }
        //如果用户输入的年份是闰年并且输入的月份大于2 那么总天数加1
        if (isfullyear(year) && month > 2) {
            num++;
        }
        return num;
    }
})
var vm = new Vue({
    el: "section",
    data: {
        now: {},
        userid: "",
        articleid: "",
        article_data: [],
        article_comments: [],
        //评论
        userMsg: "",
        dis: true,
        //组件
        show: false, //图片预览组件
        index: 0,
        images: [],
    },
    methods: {
        //图片预览
        imagePreview(images) {
            this.show = true
            this.images = images
        },
        onChange(index) {
            this.index = index;
        },
        // searchTopic
        searchTopic(topic){
            console.log(topic)
            let href = `../../../guangchang.html?topic=${encodeURIComponent(topic)}`
            window.location.href = href
            //console.log(href)
        },
        //likes
        //点赞
        good(commentId){
            



            let icon = document.querySelector("[k='"+commentId+"'] .info .time .right .Icon")
            // 禁止点击
            icon.style.pointerEvents = 'none'
            if (icon.style.backgroundImage.includes('../../../img/good-active.webp')){
                icon.style.background = 'url(../../../img/good.webp) no-repeat'
                icon.style.backgroundSize = 'contain'
                //alert("已取消")
            }else{
                icon.style.background = 'url(../../../img/good-active.webp) no-repeat'
                icon.style.backgroundSize = 'contain'
                //alert("已点赞"+icon.style.backgroundImage)
            }
            
            //console.log(icon)
            console.log(`用户【${vm.userid}】点赞了评论【${commentId}】`)
            /**/
            // return 0
            let url = `https://fc-mp-e8e2bde5-0c0c-4622-87b5-66f8b08c3856.next.bspapp.com/getArticleList1.0`
            let method = 'likes_comments_R5A4D54df5F3435'
            let data = {
                userid: vm.userid,
                commentid: commentId
            }
            axios.post(`${url}/${method}`, data)
            .then(res=>{
                //开启点击
                icon.style.pointerEvents = 'auto'
                console.log(res.data)
                if(res.data.code === 0){//请求成功
                    let likesNum = document.querySelector("[k='"+commentId+"'] .info .time .right span")
                    if(res.data.data.code === 1){//点赞操作
                        likesNum.innerText = Number(likesNum.innerText)+1
                    }else if(res.data.data.code === -1){//取消点赞操作
                        likesNum.innerText = Number(likesNum.innerText)-1
                        icon.style.background = 'url(../../../img/good.webp) no-repeat'
                        icon.style.backgroundSize = 'contain'
                    }
                }else{//请求失败
                    vm.$toast.fail({
                        message: '服务错误',
                        forbidClick: true
                    })
                    let icon = document.querySelector("[k='"+commentId+"'] .info .time .right .Icon")
                    if (icon.style.backgroundImage.includes('../../../img/good-active.webp')){
                        icon.style.background = 'url(../../../img/good.webp) no-repeat'
                        icon.style.backgroundSize = 'contain'
                        //alert("已取消")
                    }else{
                        icon.style.background = 'url(../../../img/good-active.webp) no-repeat'
                        icon.style.backgroundSize = 'contain'
                        //alert("已点赞"+icon.style.backgroundImage)
                    }
                }
            })
        },
        //评论
        checkInput(){
            if(this.userMsg !== ""){
                this.dis = false
            }else{
                this.dis = true
            }
            
        },
        
        send(){
            if(/^\s*$/.test(this.userMsg)){
                this.$toast({
                    message: '请输入内容',
                    forbidClick: true
                });
                this.userMsg = ""
                this.dis = true
            }else{
                let toast1 = this.$toast.loading({
                    //message: '加载中...',
                    forbidClick: true,
                    loadingType: 'circular',
                    duration: 4000
                  });
                console.log(`用户输入${this.userMsg}`)
                this.dis = true
                //发送请求
                let url = `https://fc-mp-e8e2bde5-0c0c-4622-87b5-66f8b08c3856.next.bspapp.com/getArticleList1.0`
                let method = 'comment_article_64F648efg48gfE45ef4E'
                let data = {
                    userid: this.userid,
                    articleid: this.articleid,
                    usermsg: this.userMsg
                }
                this.userMsg = ""
                axios.post(`${url}/${method}`, data)
                .then(res=>{
                    console.log(res.data)
                    if(res.data.code === 0) {//评论成功
                        this.dis = false
                        toast1.clear()
                        this.article_comments=res.data.data.data1
                        vm.article_data[0].comments++
                    }else{
                        this.$toast.fail('服务出错！');
                    }
                })
            }
            

            


        },

    },
    mounted() {
        let date = new Date()
        let year = date.getFullYear()
        let month = date.getMonth()+1
        let day = date.getDate()
        this.now = {
            year: year,
            month: month,
            day: day
        }
        let uid = location.search.substring(1).split('&')[0].split('=')[1]
        let id = location.search.substring(1).split('&')[1].split('=')[1]
        console.log(`uid: ${uid}`)
        console.log(`id: ${id}`)
        this.userid = uid
        this.articleid = id
        console.log(`userId: ${this.userid}`)
        let url = `https://fc-mp-e8e2bde5-0c0c-4622-87b5-66f8b08c3856.next.bspapp.com/getArticleList1.0`
        let method = 'detail_article_65EF4e8f468EF46ef51'
        let data = {
            userid: this.userid,
            articleid: this.articleid
        }
        axios.post(`${url}/${method}`, data)
        .then(res=>{
            console.log(res.data)
            this.article_data.push(res.data.data.data0)
            this.article_comments = res.data.data.data1
        })
    }
})